<template>
	<view>
		<view class="search-bg">
			<view class="head-bg"></view>
			<view class="head">
				<view class="flex flai fljcsb search">
					<view class="search-box flex flai flex1">
						<image src="../../static/img/search.png" mode="" class="search-icon"></image>
						<input type="text" value="" confirm-type="search" @confirm="search" v-model="searchCon" class="flex1" placeholder="请输入搜索内容" />
						<view class="del-box" @click="clickInput">
							<image src="../../static/img/close01.png" v-if="searchCon.length>0" mode="" class="clear-input"></image>
						</view>

					</view>
				</view>
			</view>
		</view>
		<veiw class="buy-box" v-if="buyList.length>0">
			<view class="buy-list" v-for="(item,index) in buyList" :key="index">
				<view class="buy-list-head flex flai">
					<image :src="item.avatar" mode=""></image>
					<view class="user-info">
						<text>{{item.nickname}}</text>
						<text>{{item.addTime}}</text>
					</view>
					<view @click="applyPriceHand(item.id)" class="apply" v-if="isMecahrt">报价</view>

					<!-- 商家才有报名按钮 -->
				</view>
				<view class="buy-list-con">{{item.desc}}</view>

			</view>
		</veiw>
		<view class="no-data" v-else>
			暂无求购信息
		</view>
		<view class="loading flex flai fljc" v-if="loading">
			<image src="../../static/img/loading2.gif" mode="" v-if="loadPic"></image>
			<text>{{msg}}</text>
		</view>
		<view class="public-btn flex flai fljc" @click="toPublic">
			<image src="../../static/img/public_add.png" mode=""></image>
			<text>发布</text>
		</view>
		<view style="height: 120rpx;"></view>
	</view>
</template>

<script>
	import API from '../../api/index.js'
	export default {
		data() {
			return {
				buyList: [],
				searchCon: '',
				page: 1,
				isMecahrt: uni.getStorageSync('userinfo').isMerchant,
				isPull: true,
				loading: false,
				msg: '加载中...',
				loadPic: true
			}
		},
		onShow() {
			this.buyList = []
			this.getList()
		},
		methods: {
			// 清空搜索框
			clickInput() {
				console.log('点我')
				this.searchCon = ''
				this.buyList = []
				this.getList()
			},
			toPublic() {
				uni.navigateTo({
					url: '/pages/user_public_buy/user_public_buy'
				})
			},
			search() {
				this.page = 1
				this.buyList = []
				this.getList()
			},
			getList() {
				var data = {
					keyword: this.searchCon,
					page: this.page,
					size: 10
				}
				this.loading = true
				this.msg = '加载中...'
				this.loadPic = true
				API.indexHome.buyList(data).then(res => {
					if (res.errno == '0') {
						var len = res.data.rows.length
						console.log(len)
						if (len < 10 && len > 0) {
							console.log('10')
							this.isPull = false
							this.msg = '主人我是有底线的哟~~'
							this.loadPic = false
						} else {
							this.isPull = true
							setTimeout(() => {
								this.loading = false
							}, 1000)
						}
						this.buyList = [...this.buyList, ...res.data.rows]
					}
				})
			},
			applyPriceHand(id) {
				uni.navigateTo({
					url: '/pages/shopsMarkPrice/shopsMarkPrice?id=' + id
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.buyList = []
			this.getList()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			if (this.isPull) {
				var page = this.page + 1
				console.log(page)
				this.page = page
				this.getList()
			}
		}
	}
</script>

<style>
	@import url("./index.css");
</style>
